/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package radiostation;

import javax.persistence.EntityManager;
import model.Artist;
import model.MusicGenre;
import radiostation.common.ArtistRepository;
import radiostation.common.GenreRepository;

/**
 *
 * @author profanis
 */
public class ArtistEditUI extends BaseJFrame {

    //private fields
    Artist selectedArtist;
    EntityManager entityManager;

    public ArtistEditUI(Artist artist, EntityManager entityManager) {
        this(entityManager);
        this.artist1.setArtistId(artist.getArtistId());

        // Manually
        if (artist != null) {
            birthdateDC.setDate(artist.getBirthday());
            fnameTF.setText(artist.getFirstName());
            lnameTF.setText(artist.getLastName());
            artisticNameTF.setText(artist.getArtisticName());
            birthdateDC.setDate(artist.getBirthday());
            birthPlaceTF.setText(artist.getBirthplace());
            if (artist.getSex().equals("M")) {
                sexCB.setSelectedIndex(0);
            } else {
                sexCB.setSelectedIndex(1);
            }
        }
    }

    public ArtistEditUI(EntityManager entityManager) {
        this.entityManager = entityManager;
        initComponents();

        // display in the center of the window
        setLocationRelativeTo(null);
    }

    public Artist getSelectedArtist() {
        return artist1;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        artist1 = new model.Artist();
        musicGenreQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT m.name FROM MusicGenre m");
        musicGenreList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : musicGenreQuery.getResultList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        fnameTF = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        lnameTF = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        artisticNameTF = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        sexCB = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        birthdateDC = new com.toedter.calendar.JDateChooser();
        jLabel7 = new javax.swing.JLabel();
        birthPlaceTF = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        musicGenrgeCB = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(800, 600));

        jLabel1.setText("Διαχείριση καλλιτέχνη");

        jLabel2.setText("Όνομα");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist1, org.jdesktop.beansbinding.ELProperty.create("${firstName}"), fnameTF, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel3.setText("Επίθετο");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist1, org.jdesktop.beansbinding.ELProperty.create("${lastName}"), lnameTF, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel4.setText("Καλλιτεχνικό όνομα");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist1, org.jdesktop.beansbinding.ELProperty.create("${artisticName}"), artisticNameTF, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel5.setText("Φύλλο");

        sexCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Άνδρας", "Γυναίκα" }));

        jLabel6.setText("Ημερομηνία γέννησης");

        jLabel7.setText("Καταγωγή");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist1, org.jdesktop.beansbinding.ELProperty.create("${birthplace}"), birthPlaceTF, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel8.setText("Είδος μουσικής");

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicGenreList, musicGenrgeCB);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist1, org.jdesktop.beansbinding.ELProperty.create("${musicGenre.name}"), musicGenrgeCB, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        jButton1.setText("Αποθήκευση");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveArtist(evt);
            }
        });

        jButton2.setText("Ακύρωση");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelEditing(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(fnameTF, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                            .addComponent(lnameTF)
                            .addComponent(artisticNameTF)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sexCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(birthdateDC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(musicGenrgeCB, javax.swing.GroupLayout.Alignment.LEADING, 0, 98, Short.MAX_VALUE)
                                .addComponent(birthPlaceTF, javax.swing.GroupLayout.Alignment.LEADING)))))
                .addContainerGap(42, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(101, 101, 101)
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(fnameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lnameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(artisticNameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(sexCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(30, 30, 30))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(birthdateDC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(birthPlaceTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(musicGenrgeCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(67, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveArtist(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveArtist

        //check if artistic name already exists
        ArtistRepository artistRepository = new ArtistRepository(entityManager);
        Artist foundArtist = artistRepository.findByPropertyName("ArtisticName", artisticNameTF.getText());
        if (foundArtist != null && !foundArtist.getArtistId().equals(artist1.getArtistId())) {
            showWarningMessage("Υπάρχει ήδη καλλιτέχνης με αυτό το όνομα [" + artisticNameTF.getText() + "]");
            return;
        }

        // set birthday
        artist1.setBirthday(birthdateDC.getDate());

        //set sex
        if (sexCB.getSelectedIndex() == 0) {
            artist1.setSex("M");
        } else {
            artist1.setSex("F");
        }

        //set music genre
        GenreRepository genreRepository = new GenreRepository(entityManager);
        MusicGenre selectedMusicGenre = genreRepository.findByPropertyName("Name", musicGenrgeCB.getSelectedItem().toString());
        artist1.setMusicGenre(selectedMusicGenre);

        closeFrame(true);
    }//GEN-LAST:event_saveArtist

    private void cancelEditing(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelEditing
        closeFrame(false);
    }//GEN-LAST:event_cancelEditing


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private model.Artist artist1;
    private javax.swing.JTextField artisticNameTF;
    private javax.swing.JTextField birthPlaceTF;
    private com.toedter.calendar.JDateChooser birthdateDC;
    private javax.swing.JTextField fnameTF;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField lnameTF;
    private java.util.List<model.MusicGenre> musicGenreList;
    private javax.persistence.Query musicGenreQuery;
    private javax.swing.JComboBox musicGenrgeCB;
    private javax.swing.JComboBox sexCB;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
